﻿Partial Public Class WebForm_ManageCodeMapping
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.pnlADD.GroupingText = Me.GetLocalResourceObject("pnlAddTopic")
            Me.pnlList.GroupingText = Me.GetLocalResourceObject("pnlListTopic")
            Me.BindGV()
            '判斷是否有找到
            If DirectCast(Master.FindControl("lblHeadText"), Label) IsNot Nothing Then
                DirectCast(Master.FindControl("lblHeadText"), Label).Text = Me.GetLocalResourceObject("Page.Text")
            End If
        End If
    End Sub

#Region "FormView事件"
    Protected Sub ddlTableName_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.AddHeader(TryCast(fv.FindControl("ddlTableName"), DropDownList))
    End Sub

    Protected Sub ddlColumnName_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.AddHeader(TryCast(fv.FindControl("ddlColumnName"), DropDownList))
    End Sub

    Protected Sub ddlTableName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        TryCast(Me.fv.FindControl("ddlColumnName"), DropDownList).DataBind()
        Me.BindGV()
    End Sub

    Protected Sub ddlColumnName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BindGV()
    End Sub

    Protected Sub fv_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fv.ItemInserting
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("Select * From CodeMapping ")
        sql.AppendFormat("where TableName='{0}' ", TryCast(Me.fv.FindControl("ddlTableName"), DropDownList).SelectedValue)
        sql.AppendFormat("and ColumnName='{0}' ", TryCast(Me.fv.FindControl("ddlColumnName"), DropDownList).SelectedValue)
        sql.AppendFormat("and Value='{0}' ", TryCast(Me.fv.FindControl("txtValue"), TextBox).Text)

        If dat.GetData(sql).Rows.Count > 0 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("DataExist"))
            e.Cancel = True
        End If

        Me.fv_DS.InsertParameters("TableName").DefaultValue = TryCast(Me.fv.FindControl("ddlTableName"), DropDownList).SelectedValue
        Me.fv_DS.InsertParameters("ColumnName").DefaultValue = TryCast(Me.fv.FindControl("ddlColumnName"), DropDownList).SelectedValue
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Dim ddlItem As DropDownList
        ddlItem = TryCast(fv.FindControl("ddlTableName"), DropDownList)
        ddlItem.SelectedIndex = ddlItem.Items.IndexOf(ddlItem.Items.FindByValue(gv_DS.SelectParameters("TableName").DefaultValue))
        ddlItem = TryCast(fv.FindControl("ddlColumnName"), DropDownList)
        ddlItem.DataBind()
        ddlItem.SelectedIndex = ddlItem.Items.IndexOf(ddlItem.Items.FindByValue(gv_DS.SelectParameters("ColumnName").DefaultValue))
        Me.gv.DataBind()
    End Sub
#End Region

#Region "GridView事件"
    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim ddlTableName As DropDownList = e.Row.FindControl("ddlTableName")
            If Not ddlTableName Is Nothing Then
                '編輯
                MyObj.MatchItem(ddlTableName, TryCast(e.Row.FindControl("hideTableName"), HiddenField).Value)
                TryCast(e.Row.FindControl("SqlDataSource2"), SqlDataSource).SelectParameters("TableName").DefaultValue = TryCast(e.Row.FindControl("hideTableName"), HiddenField).Value
                Dim ddlColumnName As DropDownList = e.Row.FindControl("ddlColumnName")
                ddlColumnName.DataBind()
                MyObj.MatchItem(ddlColumnName, TryCast(e.Row.FindControl("hideColumnName"), HiddenField).Value)
            End If

            Dim lblText As Label = e.Row.FindControl("lblText")
            If Not lblText Is Nothing AndAlso Me.GetLocalResourceObject("UnChange").ToString().IndexOf(lblText.Text) <> -1 Then
                TryCast(e.Row.FindControl("linkbEdit"), LinkButton).Enabled = False
                TryCast(e.Row.FindControl("linkbDelete"), LinkButton).Enabled = False
            End If
        End If
    End Sub

    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("Select * From CodeMapping ")
        sql.AppendFormat("where TableName='{0}' ", TryCast(gv.Rows(e.RowIndex).FindControl("ddlTableName"), DropDownList).SelectedValue)
        sql.AppendFormat("and ColumnName='{0}' ", TryCast(gv.Rows(e.RowIndex).FindControl("ddlColumnName"), DropDownList).SelectedValue)
        sql.AppendFormat("and Value='{0}' ", TryCast(gv.Rows(e.RowIndex).FindControl("txtValue"), TextBox).Text)
        sql.AppendFormat("and ID<>'{0}' ", gv.DataKeys(e.RowIndex).Item("ID").ToString())

        If dat.GetData(sql).Rows.Count > 0 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("DataExist"))
            e.Cancel = True
        End If
        Me.gv_DS.UpdateParameters("TableName").DefaultValue = TryCast(gv.Rows(e.RowIndex).FindControl("ddlTableName"), DropDownList).SelectedValue
        Me.gv_DS.UpdateParameters("ColumnName").DefaultValue = TryCast(gv.Rows(e.RowIndex).FindControl("ddlColumnName"), DropDownList).SelectedValue
    End Sub

    Protected Sub ddlTableName_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gvRow As GridViewRow = TryCast(TryCast(sender, DropDownList).Parent.Parent, GridViewRow)
        TryCast(gv.Rows(gvRow.RowIndex).FindControl("SqlDataSource2"), SqlDataSource).SelectParameters("TableName").DefaultValue = TryCast(sender, DropDownList).SelectedValue

    End Sub
#End Region

End Class

Partial Class WebForm_ManageCodeMapping
#Region "繫結資料"
    Public Sub BindGV()
        Me.gv_DS.SelectParameters("TableName").DefaultValue = TryCast(Me.fv.FindControl("ddlTableName"), DropDownList).SelectedValue
        Me.gv_DS.SelectParameters("ColumnName").DefaultValue = TryCast(Me.fv.FindControl("ddlColumnName"), DropDownList).SelectedValue
    End Sub
#End Region

#Region "方法"
    Private Sub AddHeader(ByVal ddlItem As DropDownList)
        ddlItem.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("All"), "0"))
    End Sub
#End Region

End Class
